Monotonic Stack
Last updated on September 12, 2024 pm
Monotonic Stack is a special type of stack.
What is a Monotonic Stack?
A monotonic stack is a special type of stack data structure in which the elements always maintain a monotonic increasing or decreasing order. When inserting a new element, the stack pops out all the elements at the top that do not satisfy the monotonicity, and then the new element is pushed onto the stack.

Types of Monotonic Stacks
Monotonic Increasing Stack
In a monotonic increasing stack, each element is larger than the one below it. When a new element is pushed onto the stack, if the top element of the stack is greater than or equal to the new element, the top element is popped out until the top element is smaller than the new element or the stack is empty.
Pseudocode
\For{$i \gets 0$ \To $|nums| - 1$}
\While{$stack$ is not empty and $nums[stack.top()] < nums[i]$}
\State $index \gets stack.pop()$
\State $result[index] \gets nums[i]$
\EndWhile
\State $stack.push(i)$
\EndFor
\State \Return $result$
\EndProcedure
\end{algorithmic}
\end{algorithm}
Java Implement
1 | |
Monotonic Decreasing Stack
In a monotonic decreasing stack, each element is smaller than the one below it. When a new element is pushed onto the stack, if the top element is smaller than or equal to the new element, the top element is popped out until the top element is greater than the new element or the stack is empty.
Pseudocode
\For{$i \gets 0$ \To $|nums| - 1$}
\Comment{Remove elements out of window}
\If{$deque$ is not empty and $deque.front() \leq i - k$}
\State $deque.pop\_front()$
\EndIf
\Comment{Maintain decreasing order in the deque}
\While{$deque$ is not empty and $nums[deque.back()] < nums[i]$}
\State $deque.pop\_back()$
\EndWhile
\State $deque.push\_back(i)$
\Comment{Store the current window maximum}
\If{$i \geq k - 1$}
\State $result[i - k + 1] \gets nums[deque.front()]$
\EndIf
\EndFor
\State \Return $result$
\EndProcedure
\end{algorithmic}
\end{algorithm}
Java Implement
1 | |
Applications of Monotonic Stack
- Next Greater/Smaller Element: Finding the next larger/smaller element to the right (or left) of each element in an array. For example, given an array, a monotonic stack can help find the next greater element for each element.
- Sliding Window Maximum: Using a monotonic decreasing stack to maintain the maximum value within a sliding window.
- Trapping Rain Water: Using a monotonic decreasing stack to efficiently calculate how much water can be trapped between pillars.
- Largest Rectangle in Histogram: Using a monotonic increasing stack to find the maximum rectangle area that can be formed by each bar in a histogram.
Advantages and Disadvantages of Monotonic Stack
Advantages
- Efficiency: The monotonic stack reduces the time complexity to $O(n)$ in many problems, as each element is pushed and popped from the stack only once. This is a significant advantage for certain problems.
- Space Saving: Compared to brute-force methods that explore all possible combinations, the monotonic stack only requires linear space.
Disadvantages
- Limited Use Cases: Monotonic stacks are mainly suitable for problems that involve sequential data processing and require updating data states based on the current element.
- Additional Space Overhead: Although it reduces time complexity, the monotonic stack requires extra space for the stack itself, which may not be suitable for memory-constrained environments.